package com.mercury.console.controller;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

public class EasyLock extends AbstractQueuedSynchronizer {
    /**
     * 锁定
     */
    public void lock(){
        acquire(1);
    }
    /**
     * 尝试锁定
     */
    public boolean tryLock(){
        return tryAcquire(1);
    }
    /**
     * 解锁
     */
    public void unlock(){
        release(1);
    }
    /**
     * 是否为锁定
     */
    public boolean isLocked(){
        return isHeldExclusively();
    }
    /**
     * 尝试获取锁
     */
    @Override
    protected boolean tryAcquire(int arg) {
        if(compareAndSetState(0,1)){
            setExclusiveOwnerThread(Thread.currentThread());
            return true;
        }
        return false;
    }
    /**
     * 尝试释放锁
     */
    @Override
    protected boolean tryRelease(int arg) {
        setExclusiveOwnerThread(null);
        setState(0);

        ConcurrentHashMap
        return true;
    }
}